package com.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.entity.UserEntity;
import com.service.UserService;

public class SpringTest {

	public static void main(String[] args) {
		test5();
	}
	
	/*****
	 * 通过xml来得到实体类  
	 * 	<bean id="userEntity" class="com.entity.UserEntity" />
	 */
	public static void test1() {
		ClassPathXmlApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		UserEntity userEntity = (UserEntity) applicationContext.getBean("userEntity");
		System.out.println(userEntity);
	}
	
	
	/*****
	 *  默认是单例模式 singleton  线程不安全
	   <bean id="user1" class="com.entity.UserEntity" scope="prototype" />
	   这里测试 prototype  返回值是false
	 */
	public static void test2() {
		ClassPathXmlApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		UserEntity userEntity1 = (UserEntity) applicationContext.getBean("user1");
		UserEntity userEntity2 = (UserEntity) applicationContext.getBean("user1");
		System.out.println(userEntity1 ==userEntity2);
	}
	
	/***
	 * 有参构造
	 * <bean id="user2" class="com.entity.UserEntity">
		<constructor-arg name="name" type="java.lang.String" value="张三"></constructor-arg>
		<constructor-arg name="age" type="java.lang.Integer" value="18"></constructor-arg>
	   </bean>
	 */
	public static void test3() {
		ClassPathXmlApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		UserEntity userEntity1 = (UserEntity) applicationContext.getBean("user2");
		System.out.println(userEntity1.getName()+"------"+userEntity1.getAge());
		
	}
	/*****
	 * 	<!-- 通过实例工厂方法创建 -->
	 *   <bean id="carFactory" class="com.entity.ObjectFactory"></bean>
         <bean id="user3" factory-bean="carFactory" factory-method="getUserEntity"></bean>
	 */
	public static void test4() {
		ClassPathXmlApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		UserEntity userEntity1 = (UserEntity) applicationContext.getBean("user3");
		System.out.println(userEntity1.getName()+"------"+userEntity1.getAge());
		
	}
	
	/****
	 * 静态工厂
	 * <bean id="user4" class="com.entity.ObjectFactory" factory-method="getStaticUserEntity"></bean>
	 */
	public static void test5() {
		ClassPathXmlApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		UserEntity userEntity1 = (UserEntity) applicationContext.getBean("user4");
		System.out.println(userEntity1.getName()+"------"+userEntity1.getAge());
	}
	
	/*** 
	 * 注入属性
	 */
	public static void test6() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		System.out.println("容器被创建..");
		UserService userService = (UserService) applicationContext.getBean("userService");
		userService.add();
	}
	
	

}
